//
//  BLPath.swift
//  BLUIKitDemo
//
//  Created by macos on 2023/4/15.
//

import UIKit

public class BLPath: NSObject{
    
    ///doc目录，持续缓存的文件应放在此目录下
    public static func documentPath() -> String?{
        let paths = NSSearchPathForDirectoriesInDomains(.documentationDirectory, .userDomainMask, true)
        return paths.first
    }
    
    ///共享目录：可以在文件App内访问的路径
    public static func shareDocumentPath() -> String? {
        return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
    }
    
    ///临时目录
    public static func tempPath() -> String? {
        return NSTemporaryDirectory()
    }

    //需要备份的数据的根目录
    public static func cacheDataRootPath() -> String?{
        return self.createPath(dirName: "cacheDataRootPath")
    }
    
    /// 创建目录
    /// - Parameters:
    ///   - rootPath: 根目录
    ///   - dirName: 新建的目录名字，可以是多个如：doc/file/phtos
    /// - Returns: 新的路径
    public static func createPath(_ rootPath:String? = nil, dirName: String?) -> String? {
        if let rp = rootPath ?? self.documentPath() {
            if dirName != nil {
                let p1 = rp + "/" + dirName!
                let fm = FileManager.default
                if fm.fileExists(atPath: p1) == false {
                    do {
                        try fm.createDirectory(atPath: p1, withIntermediateDirectories: true)
                    } catch  {
                        print("---error错误：创建目录失败---")
                    }
                }
                return p1
            }
        }
        return nil
    }
}
